package com.common.util;

import org.springframework.util.StringUtils;

import com.atomikos.icatch.Synchronization;
import com.common.util.RedisClient;
import com.common.util.SpringContextUtil;

/**
 * 序列生成
 */
public class SeqGenerateUtil {

	public static RedisClient redisClient = SpringContextUtil.getBean("redisClient");
	
	public final static String PER = "per";
	public final static String After = "after"; 
	/**
	 * 用户编号生成
	 */
	public static String userIdGenerate() {
		try {
			String per = DateUtils1.getNowDateStr2();// 当前userid前缀
			String useridper = redisClient.get("UserID");
			String useridafter = redisClient.incr("UserAfter");
			if (StringUtils.isEmpty(useridper) || !per.equals(useridper)) {
				useridper = per;
			}
			if (StringUtils.isEmpty(useridafter)) {
				useridafter = "0";//默认从0开始
			}
			redisClient.set("USEID", useridper);
			if (useridafter.length() < 8) {
				StringBuffer zero = new StringBuffer();
				for (int i = 0; i < 8 - useridafter.length(); i++) {
					zero.append("0");//不足补零
				}
				useridafter = zero + useridafter;
			}
			return useridper + useridafter;
		} catch (Exception e) {
			return null;
		}
	}
	public static String seqUtil(){
		String time = DateUtils.getNowStrDate1();
		String date = time.substring(0, 8);
		String per = redisClient.get(PER);
		String after = redisClient.incr(After);
		if(StringUtils.isEmpty(per)||!date.equals(per)){
			per = date;
			after = "0";
			redisClient.set(After,after);
		}
		redisClient.set(PER, date);
		StringBuffer bf = new StringBuffer();
		for(int i=0;i<7-after.length();i++){
			bf.append("0");
		}
		return "PR"+time+bf+after;
	}
}